package org.ligson.httptest;

import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.ligson.httptest.loginallvalue.LoginAllValue;
import org.ligson.httptest.util.UserUtil;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.atomic.AtomicInteger;

@Slf4j
public class BatchTest {
    private static final String BASE_URL = "https://jxm3qs.yonyougov.top";
    private static AtomicInteger successCount = new AtomicInteger(0);
    private static AtomicInteger totalCount = new AtomicInteger(0);


    public static void test() {
        log.debug("xxxxxxxx");
        List<String> users = UserUtil.getUserCode(2000);
        ExecutorService pool = Executors.newCachedThreadPool();

        List<Future<Boolean>> futures = new ArrayList<>();

        long startTime = new Date().getTime();
        for (String user : users) {
            LoginAllValue loginAllValue = new LoginAllValue();
            loginAllValue.setBaseUrl(BASE_URL);
            loginAllValue.setUserName(user);
            loginAllValue.setPassword("1");
            Future<Boolean> future = pool.submit(loginAllValue);
            futures.add(future);
//            CompletableFuture.supplyAsync(() -> {
//                try {
//                    return loginAllValue.call();
//                } catch (Exception e) {
//                    log.error(e.getMessage(),e);
//                    return false;
//                }
//            }).thenAccept(aBoolean -> {
//                long endTime = new Date().getTime();
//
//                totalCount.getAndIncrement();
//                if (aBoolean) {
//                    successCount.getAndIncrement();
//                }
//                log.debug("用户{}测试耗时:{}s,已执行：{}%，成功：{}%",
//                        user, (endTime - startTime) / 1000.0,
//                        totalCount.get() * 1.00 / users.size(),
//                        successCount.get() * 1.0 / totalCount.get());
//            });
        }
        List<Long> totalTime = new ArrayList<>();

        pool.execute(new Runnable() {
            @Override
            public void run() {
                while (totalCount.get() <= users.size()) {

                    for (int i = 0; i < futures.size(); i++) {
                        Future<Boolean> future = futures.get(i);
                        if (future.isDone()) {
                            int total = totalCount.incrementAndGet();
                            Boolean result = false;
                            try {
                                result = future.get();
                                log.debug("0000000000:{}:{}", total, result);
                            } catch (Exception e) {
                            }
                            int success = 0;
                            if (result) {
                                success = successCount.incrementAndGet();
                            }

                            String user = users.get(i);
                            if(!result){
                                log.error("user code:{} fail fail",user);
                            }
                            long endTime = new Date().getTime();
                            long time = (endTime - startTime);
                            log.debug("===={}:{}", total, success);
                            log.debug("用户{}({})测试耗时:{}s,已执行：{}%，成功：{}%",
                                    user, (i + 1), time / 1000.0,
                                    total * 100.00 / users.size(),
                                    success * 100.0 / users.size());

                            totalTime.add(time);
                            long totalSum = 0;
                            for (Long aLong : totalTime) {
                                totalSum += aLong;
                            }
                            log.debug("已执行:{}个,总共:{}个,平均耗时:{}s", totalTime.size(), users.size(), totalSum / (1000.0 * totalTime.size()));

                            futures.remove(future);
                        }

                    }
                }
            }
        });

    }

    public static void main(String[] args) {
//        long startTime = new Date().getTime();
//        new Thread(new Runnable() {
//            @Override
//            public void run() {
//                while (true) {
//                    long endTime = new Date().getTime();
//                    if ((endTime - startTime) > 5 * 60 * 1000) {
//
//                        break;
//                    }
//
//                }
//            }
//        }).start();

        test();
    }
}
